const http=require("http");
const fs=require("fs");
const path=require("path");
const args=process.argv.slice(2);

http.createServer((req,res)=>{
    let filePath;
    if(args.length>0){
        filePath=path.join(__dirname,args[0])
    }else{
        filePath=path.join(__dirname,'fileReader3.js');
    }
    fs.access(filePath,fs.constants.F_OK,(err)=>{
        if(err){
            res.writeHead(400,{'content-type':'text/plain'});
            res.end("文件错误");
            return;
        }
        const readStream=fs.createReadStream(filePath,'utf-8');
        readStream.on('error',(err)=>{
            res.writeHead(500,{'content-type':'text/plain'});
            res.end("错误");
        })
        //将流的数据返回给客户端
        readStream.pipe(res);
    })

}).listen(8081);

console.log("server is listening http://localhost:8081");